<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-if="loading" />
    <!-- /加载中 loading -->

    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          :src="article.aut_photo"
          round
          width="1rem"
          height="1rem"
          fit="fill"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | relative }}</p>
        </div>
        <van-button round size="small" type="info" @click="toggleFollow">
          {{ article.is_followed ? "取关" : "+ 关注" }}
        </van-button>
      </div>
      <div class="content" v-html="article.content">
        <div>正文</div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          @click="toggleLike"
          :icon="article.attitude ? 'good-job' : 'good-job-o'"
        >
          {{article.attitude === 1 ? '取消点赞' : '点赞'}}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          @click="toggleDislike"
          icon="delete"
        >
          {{ article.attitude === 0 ? '取消不喜欢': '不喜欢' }}
        </van-button>
      </div>

      <!-- 文章评论 -->
    <comment :article-id='article.art_id.toString()'></comment>
    </div>
    <!-- /文章详情 -->

  </div>
</template>

<script>
import Comment from './comment'
import { reqArticleDetail, reqFollower, reqUnFollower, reqLikings, reqDelLikings, reqAddDislikes, reqDelDislikes } from '@/api/article.js'
export default {
  name: 'ArticleIndex',
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {}
    }
  },
  components: {
    Comment
  },
  created () {
    this.getArticlesDetail()
  },
  methods: {
    // 获取文章详情
    async getArticlesDetail () {
      // id是动态路由传参 发送请求
      const res = await reqArticleDetail(this.$route.params.id)
      this.article = res.data.data
      // 结束后 关闭loading
      this.loading = false
    },
    // 切换是否关注
    async toggleFollow () {
      try {
        if (this.article.is_followed) {
          await reqUnFollower(this.article.aut_id)
          this.$toast.success('已取关')
        } else {
          await reqFollower(this.article.aut_id)
          this.$toast.success('已关注')
        }
        // 取反，根据is_followed布尔值切换是否关注
        this.article.is_followed = !this.article.is_followed
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    // 切换是否点赞
    async toggleLike () {
      try {
        // 根据attitude决定调用哪个方法 已点赞
        if (this.article.attitude === 1) {
          // 点击时取消点赞
          await reqDelLikings(this.article.art_id.toString())
          this.article.attitude = -1
        } else {
          // 添加点赞
          await reqLikings(this.article.art_id.toString())
          this.article.attitude = 1
        }
        this.$toast.success('操作成功')
      } catch (err) {
        console.log(err)
      }
    },
    // 切换是否喜欢
    async toggleDislike () {
      try {
        // 取消不喜欢
        if (this.article.attitude === 0) {
          await reqDelDislikes(this.article.art_id.toString())
          this.article.attitude = -1
        } else {
          await reqAddDislikes(this.article.art_id.toString())
          this.article.attitude = 0
        }
        this.$toast.success('操作成功')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang='scss' scoped>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
